/******************************************************************************
Copyright (c) 2013-2014, Altera Corporation.  All rights reserved.

Redistribution and use of this software, in source and binary code forms, with or without modification, are permitted provided that the foll
owing conditions are met:

1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.

2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the doc
umentation and/or other materials provided with the distribution.

3. Neither the name of Altera Corporation nor the names of its contributors may be used to endorse or promote products derived from this sof
tware without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITE
D TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER
OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF L
IABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTW
ARE, EVEN IF THE COPYRIGHT HOLDERS OR CONTRIBUTORS ARE ADVISED OF THE POSSIBILITY DAMAGE IS LIKELY TO OCCUR.
******************************************************************************/
#include <core.h>
#include <mem.h>

char buffer[128];

uint32 test_mem()
{
  int a;
  int retval = 0;
  char result1[8] = {'a', 'a', 'a', 't', 't', 'a', 'a', 'a'};
  char result2[8] = {'a', 0, 0, 0, 0, 0, 0, 'a'};
  // Test memset
  {
    memset(buffer, 'a', 8);
    memset(buffer+3, 't', 2);
    for(a=0; a<8; a++)
      if(result1[a] != buffer[a])
        retval |= 1<< a;
  }
  // Test memzero
  {
    memzero(buffer+1, 6);
    for(a=0; a<8; a++)
      if(result2[a] != buffer[a])
        retval |= (1<<8) << a;
  }
  // Test memchr
  {
    buffer[100] = 'z';
    if(memchr(buffer, 'z', 8) != NULL)
      retval |= 1<<16;
    if(memchr(buffer, 'a', 8) != buffer)
      retval |= 1<<17;
    if(memchr(buffer+1, 'a', 7) != &buffer[7])
      retval |= 1<<18;
    if(memchr(buffer, 0, 8) != &buffer[1])
      retval |= 1<<19;
    
  }
  // Test memcheck
  {
    if(memcheck(buffer, 0, 8) != buffer)
      retval |= 1<<20;
    if(memcheck(buffer, 'a', 8) != buffer+1)
      retval |= 1<<21;
    if(memcheck(buffer+1, 0, 7) != &buffer[7])
      retval |= 1<<22;
    if(memcheck(buffer+1, 0, 6) != NULL)
      retval |= 1<<23;
  }
  // Test memcpy
  {
    if(&buffer[8] != memcpy(&buffer[8], buffer, 8))
      retval |= 1<<24;
    // Copy 0-7 to 16-23
    for(a=0; a<8; a++)
      if(buffer[8+a] != result2[a])
        retval |= 1<<25;
    // Copy 1-6 to 25-30 (Memory notaligned on 32 bit boundry)
    if(&buffer[25] != memcpy(&buffer[25], &buffer[1], 6))
      retval |= 1<<26;
    for(a=0; a<6; a++)
      if(buffer[25+a] != result2[1+a])
        retval |= 1<<27;

    // Copy 1-6 to 32 (Source unaligned, Destination aligned)
    if(&buffer[32] != memcpy(&buffer[32], &buffer[1], 6))
      retval |= 1<<28;
    for(a=0; a<5; a++)
      if(buffer[32+a] != result2[1+a])
        retval |= 1<<29;
    // Copy 0-6 to 51 (Source aligned, Destination unaligned)
    memcpy(&buffer[51], &buffer[0], 6);
    for(a=0; a<6; a++)
      if(buffer[51+a] != result2[a])
        retval |= 1<<30;
    // Copy 3-7 to 65-70 (Source & Destination unaligned, different boundries)
    memcpy(&buffer[65], &buffer[3], 5);
    for(a=0; a<5; a++)
      if(buffer[65+a] != result2[3+a])
        retval |= 1u<<31;
  }
  return retval;
}

